<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: cou
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\model\ArticleType as articleTypeModel;
use think\facade\Request;
use think\facade\View;

/**
 * 文章分类管理
 * Class ArticleType
 * @package app\manage\controller
 */
class  ArticleType extends AdminController
{

    /**
     * 文章分类列表
     * @author cou
     */
    public function index()
    {
        $articleTypeModel = new ArticleTypeModel();
        if ($this->request->isAjax()) {
            return $articleTypeModel->tableData(input('param.'));
        }
        return View::fetch();
    }

    /**
     *  分类添加
     * @author cou
     */
    public function add()
    {
        $articleTypeModel = new ArticleTypeModel();
        View::layout(false);
        if ($this->request->isPost()){
            return $articleTypeModel->addData(input('param.'));
        }
        return [
            'status' => true,
            'msg'    => '获取成功',
            'data'   => View::fetch('', ['list' => $articleTypeModel->getTree()])
        ];
    }


    /**
     *  添加子分类
     * @author cou
     */
    public function addSon()
    {
        $articleTypeModel = new ArticleTypeModel();
        View::layout(false);
        $typeInfo = $articleTypeModel->where('id', input('param.id/d'))->find();
        if (!$typeInfo) return error_code(10002);
        return [
            'status' => true,
            'msg'    => '获取成功',
            'data'   => View::fetch('addson', ['typeInfo' => $typeInfo])
        ];
    }


    /**
     * 文章分类编辑
     * @author cou
     */
    public function edit()
    {
        View::layout(false);
        $articleTypeModel = new ArticleTypeModel();
        if (Request::isPost()) {
            return $articleTypeModel->editData(input('param.'));
        }
        $typeInfo = $articleTypeModel->where('id', input('param.id/d'))->find();
        if (!$typeInfo) return error_code(10002);
        return [
            'status' => true,
            'msg'    => '获取成功',
            'data'   => View::fetch('', ['typeInfo' => $typeInfo,'list' => $articleTypeModel->getTree()])
        ];
    }


    /**
     * 文章分类删除
     * @author cou
     */
    public function del()
    {
        $articleTypeModel = new ArticleTypeModel();
        $result           = ['status' => true, 'msg' => '删除成功', 'data' => ''];
        //判断该分类下是否有子分类
        if ($articleTypeModel->where('pid', input('param.id/d'))->find()) {
            $result['status'] = false;
            $result['msg']    = '该分类下有子分类无法删除';
        } else if ($articleTypeModel->comments()->where('type_id', input('param.id/d'))->find()) {
            $result['status'] = false;
            $result['msg']    = '该分类下有文章无法删除';
        } else {
            if (!$articleTypeModel->where('id', input('param.id/d'))->delete()) {
                $result['status'] = false;
                $result['msg']    = '删除失败';
            }
        }

        return $result;
    }
}
